const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')

function listFilesInDirectory(directoryPath) {
  // 用于存储所有文件的数组
  const allFiles = []

  // 使用fs.readdirSync递归遍历目录及其子目录
  function readDirectory(dir) {
      const files = fs.readdirSync(dir)

      for (const file of files) {
          const filePath = path.join(dir, file)
          const stats = fs.statSync(filePath)
          if (stats.isFile()) {
            // 只用ts格式，并排除d.ts
            if (path.extname(filePath) == '.ts' && !filePath.endsWith('.d.ts'))  allFiles.push(filePath)
          } else if (stats.isDirectory()) {
              readDirectory(filePath) // 递归处理子目录
          }
      }
  }

  readDirectory(directoryPath);

  return allFiles;
}

let files = listFilesInDirectory('./src')

// 每个ts文件生成一个json
// for(let i = 0; i < files.length; i++) {
//   let name = files[i].match(/(?<=src\/).*?(?=\.\w+)/g)[0]
//   name = name.replace(/\//g, '.')
//   execSync(`typedoc ${files[i]} --json html/document/api/${name}.json`)
// }

execSync(`typedoc ${files.join(' ')} --customCss ./api.style.css --out html/document/api/`)